package 消息队列.rabbitmq;

import com.ruoyi.charge.rabbitmq.DelayExchangeConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class ChargingBillServiceImpl implements IChargingBillService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public boolean createBill(ChargingBill chargingBill) {
        BillMessagePostProcessor billMessagePostProcessor = new BillMessagePostProcessor();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("发送时间"+simpleDateFormat.format(new Date()));

        //	public void convertAndSend(String exchange, String routingKey, final Object message,
        //			final MessagePostProcessor messagePostProcessor)
        rabbitTemplate.convertAndSend(DelayExchangeConfig.EXCHANGE_NAME,"delay.60000",chargingBill,billMessagePostProcessor);
        return true;
    }
    class BillMessagePostProcessor implements MessagePostProcessor {

        @Override
        public Message postProcessMessage(Message message) throws AmqpException {
            //设置消息延迟时间
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setHeader("x-delay",10*1000);//延迟1分钟
            return message;
        }
    }
}
